/*
 * @lc app=leetcode.cn id=3 lang=golang
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
func lengthOfLongestSubstring(s string) int {
	hm := make(map[byte]int)
	i, j := 0, 0
	maxLength := 0
	for j = 0; j < len(s); j++ {
		if index, ok := hm[s[j]]; ok && index >= i {
			maxLength = max(maxLength, j-i)
			i = index + 1
		}

		hm[s[j]] = j
	}

	return max(maxLength, len(s)-i)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

// @lc code=end

